package com.sunstar.koobaselib.kootl.comm.tool;

import android.os.Handler;
import android.os.Looper;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;

import com.socks.library.KLog;
import com.sunstar.koobaselib.kootl.comm.dialog.LoadingDialogFragment;
import com.sunstar.koobaselib.kootl.comm.dialog.LoadingDialogSmileFragment;
import com.sunstar.koobaselib.kootl.comm.dialog.OkCancelDialogFragment;
import com.sunstar.koobaselib.kootl.comm.dialog.OkDialogFragment;
import com.sunstar.koobaselib.kootl.comm.helper.DialogFragmentShowHelper;

/**
 * Created by louisgeek on 2016/12/15.
 */

public class DialogTool {
    /**
     * 共用
     */
    private static DialogFragment mLoadingDialogFragment;

    public static void showLoadingSmileDialog(FragmentActivity fragmentActivity, String msg) {
        if (fragmentActivity==null||fragmentActivity.isFinishing()||msg == null) {
            KLog.d("showLoadingSmileDialog return");
            return;
        }
        FragmentManager fragmentManager=fragmentActivity.getSupportFragmentManager();
        if (mLoadingDialogFragment != null) {
          /*  if (mLoadingDialogFragment.getActivity()==null||mLoadingDialogFragment.getActivity().isFinishing()){
                KLog.d("showLoadingDialog getActivity return");
                return;
            }*/
            if (mLoadingDialogFragment instanceof LoadingDialogSmileFragment
                    && mLoadingDialogFragment.getDialog() != null
                    ) {
                if (!mLoadingDialogFragment.getDialog().isShowing()) {
                    mLoadingDialogFragment.getDialog().show();
                }
                //都更新text
                ((LoadingDialogSmileFragment) mLoadingDialogFragment).setMessage(msg);
            } else {
                //
                //#####mLoadingDialogFragment.dismiss();
                mLoadingDialogFragment.dismissAllowingStateLoss();
                //
                mLoadingDialogFragment = LoadingDialogSmileFragment.newInstance(msg);
                ///####mLoadingDialogFragment.show(fragmentManager, "loading");
                /**
                 *show
                 */
                DialogFragmentShowHelper.show(fragmentManager,mLoadingDialogFragment,"loading");

            }
        } else {
            //为null
            mLoadingDialogFragment = LoadingDialogSmileFragment.newInstance(msg);
           // mLoadingDialogFragment.show(fragmentManager, "loading");
            DialogFragmentShowHelper.show(fragmentManager,mLoadingDialogFragment,"loading");
        }
    }

    public static void showLoadingDialog(FragmentActivity fragmentActivity, String msg) {

        if (fragmentActivity==null||fragmentActivity.isFinishing()||msg == null) {
            KLog.d("showLoadingDialog return");
            return;
        }
        FragmentManager fragmentManager=fragmentActivity.getSupportFragmentManager();
        if (mLoadingDialogFragment != null) {
         /*   if (mLoadingDialogFragment.getActivity()==null||mLoadingDialogFragment.getActivity().isFinishing()){
                KLog.d("showLoadingDialog getActivity return");
                return;
            }*/
            if (mLoadingDialogFragment instanceof LoadingDialogFragment
                    && mLoadingDialogFragment.getDialog() != null
                    ) {
                if (!mLoadingDialogFragment.getDialog().isShowing()) {
                    mLoadingDialogFragment.getDialog().show();
                }
                //都更新text
                ((LoadingDialogFragment) mLoadingDialogFragment).setMessage(msg);
            } else {
                //
                //#####mLoadingDialogFragment.dismiss();
                mLoadingDialogFragment.dismissAllowingStateLoss();
                //
                mLoadingDialogFragment = LoadingDialogFragment.newInstance(msg);
                //mLoadingDialogFragment.show(fragmentManager, "loading");
                DialogFragmentShowHelper.show(fragmentManager,mLoadingDialogFragment,"loading");
            }
        } else {
            //为null
            mLoadingDialogFragment = LoadingDialogFragment.newInstance(msg);
           // mLoadingDialogFragment.show(fragmentManager, "loading");
            DialogFragmentShowHelper.show(fragmentManager,mLoadingDialogFragment,"loading");
        }
    }

    public static void hideDialogFragment() {
        hideDialogFragment(900);
    }

    public static void hideDialogFragment(long delayMillis) {
        new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
            @Override
            public void run() {
                if (mLoadingDialogFragment != null) {
                    //#####mLoadingDialogFragment.dismiss();
                   if (mLoadingDialogFragment.getActivity()!=null
                           &&!mLoadingDialogFragment.getActivity().isFinishing()) {
                       mLoadingDialogFragment.dismissAllowingStateLoss();
                   }
                }
            }
        }, delayMillis);
    }


    /**
     * ======================================================
     */
    private static OkDialogFragment mOkDialogFragment;
    private static String mLastOkMessage;

    public static void showMsgDialog(FragmentActivity fragmentActivity, String message) {

        if (fragmentActivity==null||fragmentActivity.isFinishing()||message == null) {
            KLog.d("showMsgDialog return");
            return;
        }
        FragmentManager fragmentManager=fragmentActivity.getSupportFragmentManager();

        if (mOkDialogFragment != null) {
          /*  if (mOkDialogFragment.getActivity()==null||mOkDialogFragment.getActivity().isFinishing()){
                KLog.d("showMsgDialog getActivity return");
                return;
            }*/
            if (mOkDialogFragment.getDialog() != null && message.equals(mLastOkMessage)) {
                if (!mOkDialogFragment.getDialog().isShowing()) {
                    //#####mOkDialogFragment.dismiss();
                    mOkDialogFragment.dismissAllowingStateLoss();
                    //
                    mOkDialogFragment = OkDialogFragment.newInstance(null, message);
                    //mOkDialogFragment.show(fragmentManager, "ok");
                    DialogFragmentShowHelper.show(fragmentManager,mOkDialogFragment,"ok");
                }
            } else {
                //#####mOkDialogFragment.dismiss();
                mOkDialogFragment.dismissAllowingStateLoss();
                //
                mOkDialogFragment = OkDialogFragment.newInstance(null, message);
                //mOkDialogFragment.show(fragmentManager, "ok");
                DialogFragmentShowHelper.show(fragmentManager,mOkDialogFragment,"ok");
            }
        } else {
            mOkDialogFragment = OkDialogFragment.newInstance(null, message);
            //mOkDialogFragment.show(fragmentManager, "ok");
            DialogFragmentShowHelper.show(fragmentManager,mOkDialogFragment,"ok");
        }
        //存值
        mLastOkMessage = message;

    }

    /**
     * ======================================================
     */
    private static OkCancelDialogFragment mOkCancelDialogFragment;
    private static String mLastOkCancleMessage;

    public static void showAskDialog(FragmentActivity fragmentActivity, String message, OkCancelDialogFragment.OnBtnClickListener onBtnClickListener) {
        if (fragmentActivity==null||fragmentActivity.isFinishing()||message == null) {
            KLog.d("showAskDialog return");
            return;
        }
        FragmentManager fragmentManager=fragmentActivity.getSupportFragmentManager();
        if (mOkCancelDialogFragment != null) {
           /* if (mOkCancelDialogFragment.getActivity()==null||mOkCancelDialogFragment.getActivity().isFinishing()){
                KLog.d("showAskDialog getActivity return");
                return;
            }*/
            if (mOkCancelDialogFragment.getDialog() != null && message.equals(mLastOkCancleMessage)) {
                if (!mOkCancelDialogFragment.getDialog().isShowing()) {
                    //#####mOkCancelDialogFragment.dismiss();
                    mOkCancelDialogFragment.dismissAllowingStateLoss();
                    //
                    mOkCancelDialogFragment = OkCancelDialogFragment.newInstance(null, message);
                    mOkCancelDialogFragment.setOnBtnClickListener(onBtnClickListener);
                    //mOkCancelDialogFragment.show(fragmentManager, "okcancel");
                    DialogFragmentShowHelper.show(fragmentManager,mOkCancelDialogFragment,"okcancel");
                }
            } else {
                //#####mOkCancelDialogFragment.dismiss();
                mOkCancelDialogFragment.dismissAllowingStateLoss();
                //
                mOkCancelDialogFragment = OkCancelDialogFragment.newInstance(null, message);
                mOkCancelDialogFragment.setOnBtnClickListener(onBtnClickListener);
                //mOkCancelDialogFragment.show(fragmentManager, "okcancel");
                DialogFragmentShowHelper.show(fragmentManager,mOkCancelDialogFragment,"okcancel");
            }
        } else {
            mOkCancelDialogFragment = OkCancelDialogFragment.newInstance(null, message);
            mOkCancelDialogFragment.setOnBtnClickListener(onBtnClickListener);
            //mOkCancelDialogFragment.show(fragmentManager, "okcancel");
            DialogFragmentShowHelper.show(fragmentManager,mOkCancelDialogFragment,"okcancel");
        }
        //存值
        mLastOkCancleMessage = message;

    }
}
